/*
 * ScoreView.java
 */
package it.nesimuge.score.gui;

import it.lapomuge.db.dao.DbManagerGare;
import it.nesimuge.score.blue.BluetoothServer;
import it.nesimuge.score.db.entity.Atleta;
import it.nesimuge.score.db.entity.Gara;
import it.nesimuge.score.db.entity.Giudice;
import it.nesimuge.score.db.entity.Partecipazione;
import it.nesimuge.score.db.entity.codici.CodiceStatoPartecipazione;
import it.nesimuge.score.db.factory.ObjectFactory;
import it.nesimuge.score.gui.listener.LinstenerGareChanged;
import it.nesimuge.score.gui.listener.ListenerAtletiChanged;
import it.nesimuge.score.gui.model.ModelTableAtleti;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JButton;

/**
 * The application's main frame.
 */
public class ScoreView extends JFrame implements ListenerAtletiChanged, LinstenerGareChanged {
    
    private Gara gara = null;
    private final BluetoothServer bs;
    private final Giudice[] giudiciAttivi;
    private Partecipazione pAttiva;
    
    public ScoreView() {
        super();
        
        initComponents();
        
        this.jTermina.setEnabled(false);
        this.jAggiungiAtletaButton.setEnabled(false);
        
        JButton jNuovaGara = new JButton("New button");
        jPanel2.add(jNuovaGara);
        
        
        bs = new BluetoothServer();
     		bs.start();

     		JDialogListaGiudici g = new JDialogListaGiudici(this, true);
     		g.setModal(true);
     		g.setVisible(true);
     		this.giudiciAttivi = g.getGiudiciAttivi();
              
        
       
        this.jGiudiciLbl.setText("");
        for (Giudice giudice : giudiciAttivi) {
            this.jGiudiciLbl.setText(giudice.getCognome() + "; ");
        }
        
        
        this.getFrame().addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                bs.stopThread();
            }
        });
        
    }
    
    private JFrame getFrame() {
		return this;
	}
	

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jAtletiTable = new javax.swing.JTable();
        jNomeGara = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jAggiungiAtletaButton = new javax.swing.JButton();
        jTermina = new javax.swing.JButton();
        jNuovaGaraButton = new javax.swing.JButton();
        jSelezionaButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jGiudiciLbl = new javax.swing.JLabel();
        jVotaBtn = new javax.swing.JButton();
        
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();        
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        

        mainPanel.setName("mainPanel"); // NOI18N

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("it/nesimuge/score/gui/resources/ScoreView"); // NOI18N
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("panel.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jAtletiTable.setModel(new ModelTableAtleti());
        jAtletiTable.setName("jAtletiTable"); // NOI18N
        jScrollPane1.setViewportView(jAtletiTable);

        
        jNomeGara.setText("Nome Gara"); // NOI18N
        jNomeGara.setName("jNomeGara"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jAggiungiAtletaButton.setText(bundle.getString("gara.aggiungiatleta")); // NOI18N
        jAggiungiAtletaButton.setName("jAggiungiAtletaButton"); // NOI18N
        jAggiungiAtletaButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jAggiungiAtletaButtonMouseClicked(evt);
            }
        });
        jPanel2.add(jAggiungiAtletaButton);

        jTermina.setText(bundle.getString("gara.termina")); // NOI18N
        jTermina.setName("jTermina"); // NOI18N
        jPanel2.add(jTermina);

        jNuovaGaraButton.setText(bundle.getString("gara.nuova")); // NOI18N
        jNuovaGaraButton.setName("jNuovaButton"); // NOI18N
        jNuovaGaraButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jNuovaButtonMouseClicked(evt);
            }
        });
        jPanel2.add(jNuovaGaraButton);

        jSelezionaButton.setText(bundle.getString("gara.seleziona")); // NOI18N
        jSelezionaButton.setName("jSelezionaButton"); // NOI18N
        jSelezionaButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jSelezionaButtonMouseClicked(evt);
            }
        });
        jPanel2.add(jSelezionaButton);

        jLabel1.setText(""); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jGiudiciLbl.setText("Giudici"); // NOI18N
        jGiudiciLbl.setName("jGiudiciLbl"); // NOI18N

        jVotaBtn.setText(bundle.getString("gara.seleziona")); // NOI18N
        jVotaBtn.setName("jVotaBtn"); // NOI18N
        jVotaBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jVotaBtnMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 931, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jNomeGara)
                        .addGap(401, 401, 401)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jGiudiciLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 931, Short.MAX_VALUE)
                    .addComponent(jVotaBtn, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jNomeGara)
                    .addComponent(jLabel1)
                    .addComponent(jGiudiciLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                .addGap(1, 1, 1)
                .addComponent(jVotaBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        
        getContentPane().add(mainPanel);
        
    }// </editor-fold>//GEN-END:initComponents

private void jNuovaButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jNuovaButtonMouseClicked
    JDialogGara d = new JDialogGara(this, true);
    d.setVisible(true);
}//GEN-LAST:event_jNuovaButtonMouseClicked
    
private void jSelezionaButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSelezionaButtonMouseClicked
    JDialogGaraSeleziona d = new JDialogGaraSeleziona(this, true);    
    d.addLinstener(this);
    d.setVisible(true);
}//GEN-LAST:event_jSelezionaButtonMouseClicked
    
private void jAggiungiAtletaButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jAggiungiAtletaButtonMouseClicked
    JDialogAtleti d = new JDialogAtleti(this, true);
    d.addLinstener(this);
    d.setVisible(true);
}//GEN-LAST:event_jAggiungiAtletaButtonMouseClicked
    
private void jVotaBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jVotaBtnMouseClicked
    int rows = this.jAtletiTable.getSelectedRow();
    if (rows > -1) {
        ModelTableAtleti m = (ModelTableAtleti) this.jAtletiTable.getModel();
        pAttiva = m.getPartecipazione(rows);
        if (pAttiva.getStato() == CodiceStatoPartecipazione.NON_DISPUTATA.getCodice()) {
            ObjectFactory.inizializzaPartecipazione(pAttiva, this.giudiciAttivi);
        }
        
        
        JDialogVotazioni d = new JDialogVotazioni(this.getFrame(), true);
        d.setPartecipazione(pAttiva);
        bs.setParticepazione(pAttiva);
        bs.setParticepazioneListener(d);
        d.setVisible(true);
        
        bs.setParticepazione(null);
        
    }
}//GEN-LAST:event_jVotaBtnMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAggiungiAtletaButton;
    private javax.swing.JTable jAtletiTable;
    private javax.swing.JLabel jGiudiciLbl;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jNomeGara;
    private javax.swing.JButton jNuovaGaraButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jSelezionaButton;
    private javax.swing.JButton jTermina;
    private javax.swing.JButton jVotaBtn;
    private javax.swing.JPanel mainPanel;
    
    
    
    public void fireSelezioneCambiata(JDialogAtleti a, Atleta[] atleti) {
        a.setVisible(false);
    }
    
    public void fireSelezioneCambiata(JDialog d, Atleta[] atleti) {
        for (Atleta atleta : atleti) {
            boolean presente = false;
            for (Partecipazione p : gara.getPartecipazioneList()) {
                if (p.getAtletaId().equals(atleta)) {
                    presente = true;
                    break;
                }
            }
            
            if (!presente) {
                Partecipazione p = new Partecipazione();
                p.setAtletaId(atleta);
                p.setGaraId(gara);
                p.setStato(CodiceStatoPartecipazione.NON_DISPUTATA.getCodice());
                
                atleta.getPartecipazioneList().add(p);
                gara.getPartecipazioneList().add(p);
                
                DbManagerGare.getInstance().save(Partecipazione.class, p);
//                DbManagerGare.getInstance().save(Gara.class, gara);
//                DbManagerUtenti.getInstance().save(Atleta.class, atleta);                
            }
        }
        this.fireGaraSelected(null, gara);
        
    }
    
    public void fireGareChanged(JDialog d) {
        d.setVisible(false);
        
    }
    
    public void fireGaraSelected(JDialog d, Gara gara) {
        if (d != null) {
            d.setVisible(false);
        }
        
        this.gara = gara;
        this.jNomeGara.setText(gara.getNome());
        ModelTableAtleti model = (ModelTableAtleti) this.jAtletiTable.getModel();
        model.setGara(gara);
        
        this.jAggiungiAtletaButton.setEnabled(true);
        this.jTermina.setEnabled(true);
        
    }
}
